import { ObjCreate } from "../common/ObjCreate";
import { ImagesConst } from "../../resource/ImagesConst";
import { ResouceConst } from "../../../component/gconst/ResouceConst";
import { TilePanel } from "../equipment/TilePanel";
import { WingListRenderer } from "./WingListRenderer";
import { ScrollBarDirection } from "../../../../fl/controls/ScrollBarDirection";
import { ScrollPolicy } from "../../../../fl/controls/ScrollPolicy";
import { Cache } from "../../cache/Cache";
import { LoaderHelp } from "../../../common/display/LoaderHelp";
import { ResFileConst } from "../../resource/ResFileConst";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { ListEvent } from "../../../../fl/events/ListEvent";
import { ItemData } from "../../resource/info/ItemData";
import { KeyBoardManager } from "../../../../com/gengine/keyBoard/KeyBoardManager";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { DataProvider } from "../../../../fl/data/DataProvider";
import { GButton } from "../../../../com/mui/controls/GButton";
type int = number;
//class WingList
    
    export  class WingList /* flash.display.Sprite */
    {
        constructor()
        {
            
            this._displayListArr = [];
            this._wingListArr = [];
            this.initView();
            this.addEventListeners();
            return;
        }

        /* internal  */initView(): void
        {
            var loc1=null;
            ObjCreate.createBg(0, 0, 108, 411, this, ImagesConst.WindowCenterB);
            ObjCreate.createBg(0, 2, 104, 22, this, ImagesConst.TileBg);
            this._stBmWingList = ObjCreate.createBitmap("", 3, 3, this);
            var loc2=0;
            while (loc2 < 7) 
            {
                loc1 = ResouceConst.getScaleBitmap(ImagesConst.SplitLine);
                loc1.width = 99;
                loc1.x = 3;
                loc1.y = 68 + loc2 * 48;
                addChild(loc1);
                ++loc2;
            }
            this._wingListTilePanel = new TilePanel();
            this._wingListTilePanel.horizontalGap = 2;
            this._wingListTilePanel.verticalGap = 4;
            this._wingListTilePanel.setSize(105, 382);
            this._wingListTilePanel.columnWidth = 105;
            this._wingListTilePanel.rowHeight = 44;
            this._wingListTilePanel.setStyle("cellRenderer", WingListRenderer);
            this._wingListTilePanel.direction = ScrollBarDirection.VERTICAL;
            this._wingListTilePanel.scrollPolicy = ScrollPolicy.OFF;
            this._wingListTilePanel.move(0, 26);
            this.addChild(this._wingListTilePanel);
            this._btnListUp = ObjCreate.createGButton("", 90, 13, 17, 17, this, "PetMenuUpBtn");
            this._btnListDown = ObjCreate.createGButton("", 90, 390, 17, 17, this, "PetMenuDownBtn");
            var loc3;
            this._btnListDown.visible = loc3 = false;
            this._btnListUp.visible = loc3;
            this._backLength = Cache.instance.wardrobe.getWingList().length;
            this.updateListPanel(Cache.instance.wardrobe.getWingList());
            LoaderHelp.addResCallBack(ResFileConst.wardrobe, this.onLoadBmResource);
            return;
        }

        /* internal  */onLoadBmResource(): void
        {
            this._stBmWingList.bitmapData = GlobalClass.getBitmapData(ImagesConst.FusionCurList);
            return;
        }

        /* internal  */addEventListeners(): void
        {
            this._btnListUp.addEventListener(flash.events.MouseEvent.CLICK, this.onListBtnClick);
            this._btnListDown.addEventListener(flash.events.MouseEvent.CLICK, this.onListBtnClick);
            this._wingListTilePanel.addEventListener(ListEvent.ITEM_CLICK, this.onListItemClickHandler);
            return;
        }

        protected onListItemClickHandler(arg1: ListEvent): void
        {
            var loc1=arg1.item as ItemData;
            if (KeyBoardManager.ctrlKey) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ChatShowItem, loc1));
                arg1.stopImmediatePropagation();
                arg1.preventDefault();
                return;
            }
            this._currentListIndex = arg1.index;
            this._curWing = loc1;
            if (this._curWing) 
            {
                dispatchEvent(new DataEvent(EventName.updateWingFusion, this._curWing));
            }
            return;
        }

        protected onListBtnClick(arg1: Object /* flash.events.Event */): void
        {
            var loc1=arg1.target;
            switch (loc1) 
            {
                case this._btnListDown:
                {
                    if (this._startIndex + WingList.MAX_DISPLAY_LENGTH < this._wingListArr.length) 
                    {
                        var loc2=((loc1 = this)._startIndex + 1);
                        loc1._startIndex = loc2;
                        this._displayListArr = this.getCurrentDisplayList();
                        this._wingListTilePanel.dataProvider = new DataProvider(this._displayListArr);
                        if (this._displayListArr[(this._displayListArr.length - 1)] == this._wingListArr[(this._wingListArr.length - 1)]) 
                        {
                            this._btnListDown.visible = false;
                        }
                        this._btnListUp.visible = true;
                    }
                    break;
                }
                case this._btnListUp:
                {
                    if (this._startIndex > 0) 
                    {
                        loc2 = ((loc1 = this)._startIndex - 1);
                        loc1._startIndex = loc2;
                        this._displayListArr = this.getCurrentDisplayList();
                        this._wingListTilePanel.dataProvider = new DataProvider(this._displayListArr);
                        if (this._displayListArr[0] == this._wingListArr[0]) 
                        {
                            this._btnListUp.visible = false;
                        }
                    }
                    break;
                }
            }
            return;
        }

        /* internal  */getCurrentDisplayList(): Array<any>
        {
            var loc2=0;
            var loc3=0;
            var loc4=0;
            var loc5=0;
            var loc1=[];
            if (this._wingListArr.length <= WingList.MAX_DISPLAY_LENGTH) 
            {
                this._curWingNum = this._wingListArr.length;
                loc2 = WingList.MAX_DISPLAY_LENGTH - this._wingListArr.length;
                loc1 = this._wingListArr.concat();
                loc3 = 0;
                while (loc3 < loc2) 
                {
                    loc1.push(null);
                    ++loc3;
                }
                var loc6;
                this._btnListDown.visible = loc6 = false;
                this._btnListUp.visible = loc6;
            }
            else 
            {
                this._curWingNum = WingList.MAX_DISPLAY_LENGTH;
                loc4 = this._startIndex + WingList.MAX_DISPLAY_LENGTH;
                loc5 = this._startIndex;
                while (loc5 < loc4) 
                {
                    loc1.push(this._wingListArr[loc5]);
                    ++loc5;
                }
                if (loc4 < this._wingListArr.length && !this._btnListDown.visible) 
                {
                    this._btnListDown.visible = true;
                }
            }
            return loc1;
        }

        public updateListPanel(arg1: Array<any>, arg2: int=2): void
        {
            this._type = arg2;
            this._wingListArr = arg1;
            var loc1=new DataProvider();
            var loc2=this._wingListArr.length;
            this._backLength = this._wingListArr.length;
            this._displayListArr = this.getCurrentDisplayList();
            this._wingListTilePanel.dataProvider = new DataProvider(this._displayListArr);
            return;
        }

        /* internal */ static /* const */MAX_DISPLAY_LENGTH: int=8;

        /* internal */ /* var */_wingListTilePanel: TilePanel;

        /* internal */ /* var */_btnListUp: GButton;

        /* internal */ /* var */_btnListDown: GButton;

        /* internal */ /* var */_startIndex: int=0;

        /* internal */ /* var */_curWingNum: int=0;

        /* internal */ /* var */_displayListArr: Array<any>;

        /* internal */ /* var */_wingListArr: Array<any>;

        /* internal */ /* var */_stBmWingList: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_currentListIndex: int=0;

        /* internal */ /* var */_curWing: ItemData;

        /* internal */ /* var */_backLength: int;

        /* internal */ /* var */_type: int;
    }
